import axios from "axios";
import store from "@/store";
import { Message } from "element-ui";

const request = axios.create({
  baseURL: "https://likede2-admin.itheima.net/likede/api/",
  timeout: 8000,
});

request.interceptors.request.use(
  (config) => {
    const token = store.getters.token;
    config.headers["authorization"] = token;
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

request.interceptors.response.use(
  (response) => {
    // console.log(response)
    return response.data;
  },
  (err) => {
    if (err.response.status === 401) {
      Message.error("尊敬的用户，当前登录状态已过期！");
      store.commit("/login");
    } else {
      Message.error(err.response.data.message);
    }
    return Promise.reject(err);
  }
);

export default request;
